package com.fhz.day018.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;


import com.fhz.day018.Song;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间:2022/5/9
 *
 * @author msfeng
 * 简述: TODO
 *
 * //1,饿汉式. 一上来就创建对象.
 * //2,懒汉式,什么时候调用方法的时候,什么时候创建对象.
 *
 */
public class SongDataUtil {

    //1 私有构造
    private SongDataUtil() {
    }

//    //2 提供一个成员变量
//    private static SongDataUtil songDataUtil = new SongDataUtil();
//
//    //3.给外部提一个方法.来返回当前的对象.
//    public static SongDataUtil getSongDataUtil(){
//        return songDataUtil;
//    }

    //2,提供一个成员变量
    private static SongDataUtil songDataUtil = null;

    //3.给外部提一个方法.来返回当前的对象.
    public static SongDataUtil getSongDataUtil(){
        if(songDataUtil == null){
            synchronized (SongDataUtil.class){
                if(songDataUtil == null){
                    return songDataUtil = new SongDataUtil();
                }
            }
        }
        return songDataUtil;
    }

    public List<Song> getDataList(Context context){
        List<Song> list = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        String[] strings = new String[]{MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.DATA};
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, strings, null, null, null);
        while (query.moveToNext()){
            String title = query.getString(query.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            String duration = String.valueOf(query.getString(query.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
            String album_id = String.valueOf(query.getString(query.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)));
            String data = query.getString(query.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            Song song = new Song(album_id,title,duration,data);
            list.add(song);
        }
        return list;
    }


    public static String getAlbum(Context context,int id){
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(Uri.parse(mUriAlbums + "/" + id), projection, null, null, null);
        if (query != null) {
            //过滤一下短音频
            if (query.getCount() > 0 && query.getColumnCount() > 0) {
                query.moveToNext();
                String string = query.getString(0);

                return string;
            }
            query.close();
        }
        return null;

    }

    //格式化时间
    public static String formatTime(int time) {
        //3:12 . //3:09
        if (time / 1000 % 60 < 10) {
            return time / 1000 / 60 + ":0" + time / 1000 % 60;
        } else {
            return time / 1000 / 60 + ":" + time / 1000 % 60;
        }
    }


}

